﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Lemonc.Tools
{
    public class XmlTools
    {
        /// <summary>
        /// xml文件生成类
        /// </summary>
        /// <param name="xmlfilepath">xml文件路径</param>
        /// <param name="type">类的类型</param>
        /// <returns></returns>
        public static object DeserializeFromXml(string xmlfilepath, Type type)
        {
            object result = null;
            if (File.Exists(xmlfilepath))
            {
                using (StreamReader reader = new StreamReader(xmlfilepath))
                {
                    XmlSerializer xs = new XmlSerializer(type);
                    result = xs.Deserialize(reader);
                }

            }
            return result;
        }


        public static XmlDocument XmlStrToDocument(string xmlstr)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(xmlstr);
            return xml;
        }
        public static string getXmlElementStr(XmlDocument xml, string node)
        {
            XmlElement add = (XmlElement)xml.SelectSingleNode(node);
            return add.InnerText ;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool setXmlElement(string xmlPath, string key, string value)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlPath);
            XmlElement adds = (XmlElement)xml.SelectSingleNode("SiteConfig/add");
            while (1 == 1)
            {
                string s = adds.GetAttribute("key").ToString();
                if (s == key)
                {
                    adds.SetAttribute("value", value);
                    break;
                }
                else { adds = (XmlElement)adds.NextSibling; }
            }
            xml.Save(xmlPath);
            return true;
        }

      
      

        public bool SetXmlElement(string xmlPath, string node, string key, string value)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlPath);
            XmlElement adds = (XmlElement)xml.SelectSingleNode(node);
            while (1 == 1)
            {
                string s = adds.GetAttribute("key").ToString();
                if (s == key)
                {
                    adds.SetAttribute("value", value);
                    break;
                }
                else { adds = (XmlElement)adds.NextSibling; }
            }
            xml.Save(xmlPath);
            return true;
        }
        /// <summary>
        /// 插入标签<add key="obj" msg="" ....>
        /// </summary>
        /// <param name="xmlPath">xml路径</param>
        /// <param name="node">xml插入路径</param>
        /// <param name="obj">类</param>
        /// <returns></returns>
        public bool AddXmlElement(string xmlPath, string node, Object obj)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlPath);
            string[] ss = node.Split('/');
            XmlElement objElement = xml.CreateElement(ss[ss.Length - 1]);
            PropertyInfo[] pis = obj.GetType().GetProperties();
            XmlAttribute key = xml.CreateAttribute("key");
            key.InnerText = Convert.ToString(obj.GetType().Name);//类名
            objElement.Attributes.Append(key);
            foreach (PropertyInfo pi in pis)
            {
                object value1 = pi.GetValue(obj, null);
                XmlAttribute xa = xml.CreateAttribute(pi.Name);
                xa.InnerText = Convert.ToString(value1.ToString());
                objElement.Attributes.Append(xa);
            }
            string nodeStr = ss[0];
            for (int i = 1; i < ss.Length - 1; i++)
            {
                nodeStr = nodeStr + "/" + ss[i];
            }
            XmlElement adds = (XmlElement)xml.SelectSingleNode(nodeStr);
            adds.AppendChild(objElement);
            xml.AppendChild(adds);
            xml.Save(xmlPath);
            return true;
        }
        public bool SetXmlElement(string xmlPath, string node, Object obj)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlPath);
            PropertyInfo[] pis = obj.GetType().GetProperties();
            XmlElement objElement = (XmlElement)xml.SelectSingleNode(node);
            while (1 == 1)
            {
                string s = objElement.GetAttribute("key").ToString();
                if (s == obj.GetType().Name)
                {
                    foreach (PropertyInfo pi in pis)
                    {
                        object value1 = pi.GetValue(obj, null);
                        objElement.SetAttribute(pi.Name, value1.ToString());
                    }
                    break;
                }
                else { objElement = (XmlElement)objElement.NextSibling; }
            }
            xml.Save(xmlPath);
            return true;
        }
        public bool DelXmlElement(string xmlPath, string node, string keyname)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(xmlPath);
            XmlElement objElement = (XmlElement)xml.SelectSingleNode(node);
            string[] ss = node.Split('/');
            string nodeStr = ss[0];
            for (int i = 1; i < ss.Length - 1; i++)
            {
                nodeStr = nodeStr + "/" + ss[i];
            }

            XmlElement adds = (XmlElement)xml.SelectSingleNode(nodeStr);
            while (1 == 1)
            {
                string s = objElement.GetAttribute("key").ToString();
                if (s == keyname)
                {
                    XmlNode delnode = (XmlNode)objElement;
                    XmlNode fjnode = delnode.ParentNode;//找到父节点
                    fjnode.RemoveChild(delnode);//删除当前节点
                    break;

                }
                else { objElement = (XmlElement)objElement.NextSibling; }
            }
            xml.Save(xmlPath);
            return true;
        }

        public static object Deserialize(Type type, string s)
        {
            using (StringReader sr = new StringReader(s))
            {
                XmlSerializer xz = new XmlSerializer(type);
                return xz.Deserialize(sr);
            }
        }
    }
}
